xquery version "1.0";

declare namespace request="http://exist-db.org/xquery/request";
declare namespace response="http://exist-db.org/xquery/response";
declare namespace session="http://exist-db.org/xquery/session";
declare namespace xdb="http://exist-db.org/xquery/xmldb";

import module namespace ranking="http://www.example.com/ranking" at "xmldb:exist:///db/projdb/modules/ranking_mod.xqm";

(: type of result document = text/XML :)
declare option exist:serialize "method=xml media-type=text/xml omit-xml-declaration=yes indent=yes";

let $periods := ('2009-2010', '2010-2011', '2011-2012', '2012-2013', '2013-2014')
return element values {
for $period at $position in $periods
    let $doc := doc(concat('/db/projdb/participatie_parameters_ge2009_', $period, '.xml'))
    let $ranking := ranking:create_partialRankingList($doc)
    let $maxRanking := max($ranking//li/@ranking)
    let $avgRanking := avg($ranking//li/@ranking)
    let $avgValue := avg(data($ranking//li/value))
    let $item := $ranking//li[data(@ref) = request:get-parameter("id", 0)]
    return <period><label>{$period}</label><value>{data($item/value)}</value><ranking>{data($item/@ranking)}</ranking><max>{$maxRanking}</max><avgValue>{$avgValue}</avgValue><avgRanking>{$avgRanking}</avgRanking></period>
}
